package net.maku.system.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.maku.framework.common.page.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.system.convert.OperationLogConvert;
import net.maku.system.entity.OperationLogEntity;
import net.maku.system.query.OperationLogQuery;
import net.maku.system.service.OperationLogService;
import net.maku.system.vo.OperationLogVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
* 皂搓日志
*
* @author yebing 
* @since 1.0.0 2023-07-29
*/
@RestController
@RequestMapping("operationlog")
@Tag(name="皂搓日志")
@AllArgsConstructor
public class OperationLogController {
    private final OperationLogService operationLogService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('operationlog:page')")
    public Result<PageResult<OperationLogVO>> page(@Valid OperationLogQuery query){
        PageResult<OperationLogVO> page = operationLogService.page(query);

        return Result.ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('operationlog:info')")
    public Result<OperationLogVO> get(@PathVariable("id") Long id){
        OperationLogEntity entity = operationLogService.getById(id);

        return Result.ok(OperationLogConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('operationlog:save')")
    public Result<String> save(@RequestBody OperationLogVO vo){
        operationLogService.save(vo);

        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    @PreAuthorize("hasAuthority('operationlog:update')")
    public Result<String> update(@RequestBody @Valid OperationLogVO vo){
        operationLogService.update(vo);

        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('operationlog:delete')")
    public Result<String> delete(@RequestBody List<Long> idList){
        operationLogService.delete(idList);

        return Result.ok();
    }
}